package com.chg.mymvc.servlet;

import com.chg.mymvc.entity.Item;
import com.chg.mymvc.entity.News;
import com.chg.mymvc.service.ItemServiceImpl;
import com.chg.mymvc.service.NewsServiceImpl;
import com.chg.mymvc.util.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/nmanage.html")
public class NMangeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
//为了方便下拉框展示，在点击新闻管理的时候就把值获取到并且存下来
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        ItemServiceImpl isi = new ItemServiceImpl();
        List<Item> items = isi .getAllItem();
        System.out.println(items);
        req.getSession().setAttribute("itemsofperm",items);



        NewsServiceImpl is = new NewsServiceImpl();


        //分页
        //获取用户提交的当前页码
        int pagesize=5;
        String num=req.getParameter("pagenum");
        int pagenum=num==null? 1:Integer.parseInt(num);


//创建分页工具
        Page page = new Page();
        page.setPageNum(pagenum);
        page.setPageSize(pagesize);
//获取总条数
        page.setTotal(is.allcount());

//传list为了展示
        List<News> list = is.queryAll(page.getPageNum(), page.getPageSize());

        page.setList(list);
        req.setAttribute("ilist",list);
        req.setAttribute("page",page);



        req.getRequestDispatcher("page/NewsListManagement.jsp").forward(req,resp);

//        req.getRequestDispatcher("page/NewsManagement.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
